<template>
  <div class="lyl-table">
    <div class="header">
      <div class="title">{{ title }}</div>
      <div class="handel">
        <slot name="headerHandler"></slot>
      </div>
    </div>
    <el-table
      border
      style="width: 100%"
      :data="dataList"
      v-bind="childrenProps"
    >
      <el-table-column
        type="selection"
        align="center"
        width="60"
        v-if="showSelectColumn"
      ></el-table-column>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="80"
        v-if="showIndexColumn"
      ></el-table-column>
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <template #default="scope">
            <slot :name="propItem.slotName" :row="scope.row">{{
              scope.row[propItem.prop]
            }}</slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer">
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        />
      </slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from 'vue'

const props = defineProps({
  // table模板
  propList: {
    type: Array,
    required: true
  },
  // table标题
  title: {
    type: String,
    default: ''
  },
  // table数据
  dataList: {
    type: Array,
    required: true
  },
  // 分页器总数
  listCount: {
    type: Number,
    requried: true
  },
  // 传递的分页器初始化数据
  page: {
    type: Object,
    default: () => ({ currentPage: 1, pageSize: 10 })
  },
  // 表格勾选
  showSelectColumn: {
    type: Boolean,
    default: true
  },
  // 表格序号
  showIndexColumn: {
    type: Boolean,
    default: true
  },
  // 展开列的属性
  childrenProps: {
    type: Object,
    default: () => ({})
  }
})
const emit = defineEmits(['update:page'])

/**
 * handleCurrentChange 页码改变的回调
 * @param currentPage 当前页码
 */
const handleCurrentChange = (currentPage: number) => {
  emit('update:page', { ...props.page, currentPage })
}
/**
 * handleSizeChange 每页显示数据条数变化的回调
 * @param pageSize 当前每页显示数据的条数
 */
const handleSizeChange = (pageSize: number) => {
  emit('update:page', { ...props.page, pageSize })
}
</script>

<style scoped lang="less">
.header {
  display: flex;
  justify-content: space-between;
  padding-bottom: 10px;
}
.footer {
  margin-top: 10px;
  .el-pagination {
    justify-content: end;
  }
}
</style>
